/* DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ics414scuba;
        
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.ArrayList;

/**
 * DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
 * Class to store the dive schedule of the user. It also writes and reads files that contain the dive information.
 * @author Elliot Ito
 */
public class DiveSchedule {
    private final ArrayList<DiveObject> diveData;
    private int diveCount = 0;
    private int breakCount = 0;
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Constructor for the class, initalizes the ArrayList.
     */
    public DiveSchedule(){
        diveData = new ArrayList<>();
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Addes dive to the arraylist. Increments the dive count and prints out data for checking.
     *
     * @param depth Depth of the dive
     * @param time Time for dive
     * @param diveGroup The dive group of the dive
     */
    public void addDive(String depth, String time, char diveGroup){
        this.diveCount++;
        diveData.add(new DiveObject("dive"+this.diveCount, depth, time, diveGroup));
        System.out.println("Added: " +new DiveObject("dive"+this.diveCount, depth, time, diveGroup));
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Adds break to the arrayList. Increments the breakCount and prints out data for checking.
     *
     * @param time Time of the break.
     * @param breakGroup The breakGroup of the break
     */
    public void addBreak(String time, char breakGroup){
        this.breakCount++;
        diveData.add(new DiveObject("break"+Integer.toString(this.breakCount), time, breakGroup));
         System.out.println("Added: " +new DiveObject("break"+Integer.toString(this.breakCount), time, breakGroup));
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Returns instance of the diveData list.
     * @return Instance of the diveData list.
     */
    public ArrayList<DiveObject> returnList(){
        ArrayList<DiveObject> returnList = this.diveData;
        return returnList;
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Removes dive/break from the dive table given the index of the dive/break. If a break is selected, the dive after it is deleted, and if a dive is selected, the break before it is deleted.
     * @param index
     */
    public void removeDive(int index){
        if(this.diveData.get(index).returnType().equals("break")){
            this.diveData.remove(index+1);
            this.diveData.remove(index);   
        }
        else{
            this.diveData.remove(index);
            this.diveData.remove(index-1);
        }
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Returns the list as a string.
     * @return A formatted String of the list.
     */
    public String printList(){
        String print = "";
        for (DiveObject diveData1 : this.diveData) {
            print+=(diveData1.toString() + "\n");
        }
        return print;
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Makes file for the dive.
     * @param fileName Name/Location of the file
     * @throws IOException
     */
    public void makeFile(File fileName) throws IOException{
        Charset charset = Charset.forName("utf-8");
        Writer writer = null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(
                  new FileOutputStream(fileName), charset));
            writer.write(printList());
        } catch (IOException ex) {
          // report
        } finally {
           try {
               writer.close();
           } catch (Exception ex) {}
        }
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Returns boolean value if the schedule is empty
     * @return Bool if the diveData is empty
     */
    public boolean isEmpty(){
        if(this.diveData.isEmpty()){
            return true;
        }else return false;
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * returns last dive object
     * @return Last dive object.
     */
    public DiveObject getLast(){
        DiveObject returnObject = this.diveData.get(this.diveData.size() - 1);
        System.out.println(returnObject);
        return returnObject;
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Rechecks if the diveTable is valid
     * @return
     */
    public boolean recheck(){
        //loop though arraylist and check if there are any issues with the dive
        return false;
    }
    
    
    private void lineReader(String fileLine){
        String[] parser = fileLine.split("\\s");
        if(fileLine.contains("dive")){
            addDive(parser[1],parser[2],parser[3].charAt(0));
        }
        else {
            addBreak(parser[1],parser[2].charAt(0));
        }
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Reads file that contains diveTable
     * @param fileName Name/Location of the file
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void readFile(File fileName) throws FileNotFoundException, IOException {
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String line = null;
            String fileContents = "";
            String[] parsed;
            while((line = reader.readLine()) != null){
                fileContents += line;
            }
            parsed = fileContents.split("\n");
        for (String parsed1 : parsed) {
            lineReader(parsed1);
        }
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Prints the number of dives and the number of breaks.
     */
    public void printStatus(){
        System.out.println("Number of Dives "+this.diveCount+"\n"+"Number of breaks "+this.breakCount);
    }
    
    /**DISCLAIMER: This is a PROTOTYPE and must NOT be used to plan real dives.
     * Clears the diveData.
     */
    public void clearDive(){
        this.diveData.clear();
    }
           
}
